/**
 * Created by gongxun on 16/11/18.
 */

var KwxRoomJoinLayer = KwxSecondBase.extend({
    Panel_center : null,
    mImageNums : null,
    mInputStr : null,
    ctor : function () {
        this._super();
        this.loadSecondCCS("res/ccs/KwxRoomJoinLayer.json");
        this.setTitleText("res/ccs/hall_second/text/text_jiarufangjian.png");

        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        this.mInputStr = "";
    },

    onJoinRoom : function(roomId){
        var roomNum = parseInt(roomId);

        //kwx   100001-199999
        //nn    200001-299999
        //gsbdx 400001-499999
        //psz   500001-599999
        //whmj  300001-399999
        //kwxpp 600001-699999
        //dzpk  700001-799999
        //dtj   800001-899999

        var temp = {};
        temp.kwx = 100001;
        temp.nn = 200001;
        temp.whmj = 300001;
        temp.gsbdx = 400001;
        temp.psz = 500001;
        temp.kwxpp = 600001;
        temp.dzpk = 700001;
        temp.dtj = 800001;
        temp.pdk = 900001;

        var checkRoomIds = [];
        if (mo.gameHelper.projectCode == 0){
            //TODO
            checkRoomIds = [
                "kwx",
                "nn",
                "gsbdx",
                "psz",
                "kwxpp",
                "dzpk",
                "dtj",
                "pdk"
            ];
        }else if (mo.gameHelper.projectCode == 1){
            checkRoomIds = [
                "kwx",
                "psz",
                "kwxpp"
            ];
        }else if (mo.gameHelper.projectCode == 2){
            checkRoomIds = [
                "kwx",
                "nn",
                "psz",
                "whmj",
                "kwxpp",
                "dzpk"
            ];
        }

        var isCheck = false;
        for (var i = 0; i < checkRoomIds.length; i++){
            var gameName = checkRoomIds[i];
            var roomBegan = temp[gameName];
            var roomEnd = roomBegan + 99998;
            if (roomNum >= roomBegan && roomNum <= roomEnd){

                //进入房间等待画面
                mo.showWaitCreateTableLayer(gameName);

                //发送进入房间消息
                mo.gameUserInfo.sendJoinRoom(gameName, roomNum);
                isCheck = true;
            }
        }

        if (!isCheck){
            mo.showRoomMsgShow("加入牌局失败，牌桌号不存在");
        }
    },

    onEnter : function(){
        this._super();

        this.loadUI();
    },

    closeThis : function(){
        this.removeFromParent();
    },

    loadUI : function(){
        this.mImageNums = [];
        for (var i = 0; i < 6; i++){
            this.mImageNums.push(mo.seekWidgetByName(this.ccsNode, "Text_inputNum" + i));
        }
        this.updateInput();

        //添加
        for (var i = 0; i < 10; i++){
            var button = mo.seekWidgetByName(this.ccsNode, "Button_num" + i);
            button.setTag(i);
            var AtlasLabel_num = mo.seekWidgetByName(button, "AtlasLabel_num");
            AtlasLabel_num.setString(i + "");

            button.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var num = sender.getTag();
                    this.addInputNum(num);
                }
            }, this);
        }

        //删除
        mo.seekWidgetByName(this.ccsNode, "Button_del").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.subInputNum();
            }
        }, this);

        //清空
        mo.seekWidgetByName(this.ccsNode, "Button_reset").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.cleanInputNum();
            }
        }, this);
    },

    updateInput : function(){
        for (var i = 0; i < 6; i++){
            var lenght = this.mInputStr.length;
            if (i < lenght){
                this.mImageNums[i].setVisible(true);
                this.mImageNums[i].setString(this.mInputStr.charAt(i) + "");
            }else{
                this.mImageNums[i].setVisible(false);
            }
        }
    },

    addInputNum : function(num){
        this.mInputStr += (num + "");

        if (this.mInputStr.length == 6){
            this.onJoinRoom(this.mInputStr);
            this.closeThis();
        }else{
            this.updateInput();
        }
    },

    subInputNum : function(){
        if (this.mInputStr.length > 0){
            this.mInputStr = this.mInputStr.substr(0, this.mInputStr.length - 1);
            this.updateInput();
        }
    },

    cleanInputNum : function(){
        this.mInputStr = "";
        this.updateInput();
    }
});